﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace PRE
{
    public partial class frmGoimon : PRE.Catalog.FrmBase
    {
        DAL.EntitiesDAL.chitietphieuDAL _dal = new DAL.EntitiesDAL.chitietphieuDAL();
        BLL.EntitiesBLL.chitietphieuBLL _bll = new BLL.EntitiesBLL.chitietphieuBLL();
        public frmGoimon()
        {
            InitializeComponent();
        }
        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
           
            ClearDataBindings();
            string _dd = _dal.Select_maphieu(cbTenban.EditValue + "");
            string _bien = cbTenban.EditValue + "";
            _dtb = _dal.loc_tenban(_bien);
            dsthongke.DataSource = _dtb;
            DataBindingControl();
            //if (_dtb.Rows.Count == 0)
            //{
            cbbxmp.EditValue = _dd + "";
            cbbxmp.Text = _dd + "";
                //ClearDataBindings();
            //}
            //DataTable _ma = _dal.Select_max();
            //if (_ma != null)
            //    if (_ma.Rows.Count > 0)
            //    {
            //        string ma = _bll.AddID(_ma.Rows[0]["id"] + "");
            //        txtmamon.Text = ma;
            ReadOnlyControl(false);
            ClearDataBindings();
            //
            
            //
            ChangeStatus(false);
            //cbbxmp.ResetText();
            cbomamon.ResetText();
            cboNV.ResetText();
            txtSL.ResetText();
            txtDongia.ResetText();
            txtGG.ResetText();
            txtThanhtien.ResetText();
           
            //}


            base.PerformAdd();
        }
        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }
        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = cbbxmp.Text;
            string pass = cbomamon.EditValue + "";
            //string pass = cbomamon.Text;

            DialogResult result;
            var ok = _dal.xoa(id, pass);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus(false);
            cbomamon.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            
            if (IsAdd)
            {
                var o = new DAL.Entities.chitietphieu()
                {
                    MAPHIEU = cbbxmp.EditValue + "",
                    MAMON = cbomamon.EditValue + "",
                    MANV = cboNV.EditValue + "",
                    SOLUONG  = txtSL.Text ,
                    DONGIA = txtDongia.Text ,
                    GIAMGIA = txtGG.EditValue + "",
                    
                    TINHTIEN = txtThanhtien.Text,
                    


                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                //var _mabn = grvMain.GetFocusedRowCellValue("MaBN") + "";
                //string _madichvu = Text;
                var o = new DAL.Entities.chitietphieu()
                {
                    MAPHIEU = cbbxmp.EditValue + "",
                    MAMON = cbomamon.EditValue + "",
                    MANV = cboNV.EditValue + "",
                    SOLUONG = txtSL.Text,
                    DONGIA = txtDongia.Text,
                    GIAMGIA = txtGG.EditValue + "",

                    TINHTIEN = txtThanhtien.Text,
                    
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txtSL.Text = null;
            txtDongia.Text = null;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
                    
            cbbxmp.DataBindings.Clear();
            cbomamon.DataBindings.Clear();
            cboNV.DataBindings.Clear();
            txtSL.DataBindings.Clear();
            txtDongia.DataBindings.Clear();
            txtGG.DataBindings.Clear();
            txtThanhtien.DataBindings.Clear();
           
            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()

        {
                   
            cbbxmp.DataBindings.Add("EditValue", _dtb, ".MAPHIEU");
            cbomamon.DataBindings.Add("EditValue", _dtb, ".MAMON");
            cboNV.DataBindings.Add("EditValue", _dtb, ".MANV");
            txtSL.DataBindings.Add("EditValue", _dtb, ".SOLUONG");
            txtDongia.DataBindings.Add("EditValue", _dtb, ".DONGIA");
            txtGG.DataBindings.Add("EditValue", _dtb, ".GIAMGIA");
            txtThanhtien.DataBindings.Add("EditValue", _dtb, ".TINHTIEN");
            
            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            cbbxmp.Properties.ReadOnly = isReadOnly;
            cbomamon.Properties.ReadOnly = isReadOnly;
            cboNV.Properties.ReadOnly = isReadOnly;
            txtSL.Properties.ReadOnly = isReadOnly;
            txtDongia.Properties.ReadOnly = isReadOnly;
            txtGG.Properties.ReadOnly = isReadOnly;
            txtThanhtien.Properties.ReadOnly = isReadOnly;
            dsthongke.Enabled = isReadOnly;
            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //string _bien = cbTenban.EditValue + "";
            _dtb = _dal.Select();
            //_dtb = _dal.loc_tenban(_bien);


            if (_dtb != null)
            {
                dsthongke.DataSource = _dtb;
                //string _dem1 = _dal.count_();
                //thongke.Text = "Tổng số có  " + _dem1.ToString() + " nhân viên";
            }
            base.LoadData();
        }
        #endregion

        private void frmGoimon_Load(object sender, EventArgs e)
        {
            DataTable _DDL = _dal.mon();
            cbomamon.Properties.DataSource = _DDL;

            DataTable _tenban = _dal.load_ban();
            cbTenban.Properties.DataSource = _tenban;

            DataTable _dll = _dal.tenNV();
            cboNV.Properties.DataSource = _dll;

            DataTable _all = _dal.maphieu();
            cbbxmp.Properties.DataSource = _all;

            //string _makhoa = cbTenban.EditValue + "";
            //DataTable LOC = _dal.loc_tenban(_makhoa);
            //dsthongke.DataSource = LOC;

            //cbTenban_EditValueChanged( sender,  e);


            //string _ddd = _dal.Select_dongia(cbomamon.Text);
            //txtDongia.Text = _ddd + "";

            //string _dem = _dal.sum(txtSTTPDH.Text.Trim());
            //thongke.Text = _dem.ToString();

            //DataBindingControl1();
            
        }

        protected void DataBindingControl1()
        {

            //txtDongia.DataBindings.Add("EditValue", _dtb, ".dongia");
            //

        }

        private void cbomamon_EditValueChanged(object sender, EventArgs e)
        {
            if(cbomamon.Text !=null)
            {
            string _ddd = _dal.Select_dongia(cbomamon.EditValue + "");
            txtDongia.Text = _ddd + "";
            }
            else
            {
                cbomamon.Text = null;
                txtDongia.Text = null;
            }
        }

        private void txtSL_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                float a, b, c;
                a = float.Parse(txtSL.Text);
                b = float.Parse(txtDongia.Text);
                c = float.Parse(txtGG.Text);
                
                    txtThanhtien.Text = ((a * b) - (a * b * (c/ 100))).ToString();
             
                

                //if (a > b)
                //{
                //    a = 0;
                //    MessageBox.Show("Số lượng bạn cần mua đã vượt quá số lượng cho phép! Số lượng hiện tại là " + b + " sản phẩm", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    textBox1.Text = "0";
                //}
            }
            catch
            {
                //MessageBox.Show("Mời bạn mức giảm giá!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //txtSL.Text = "0";
            }
        }

        private void txtGG_SelectedIndexChanged(object sender, EventArgs e)
        {
          
              txtSL_EditValueChanged(sender, e);
           
        }

        private void cbTenban_EditValueChanged(object sender, EventArgs e)
        {
            string _dd = _dal.Select_maphieu(cbTenban.EditValue + "");
           
            if (cbomamon.Text != null)
            {
                string _ddd = _dal.Select_dongia(cbomamon.EditValue + "");
                txtDongia.Text = _ddd + "";
            }
            else
            {
                cbomamon.Text = null;
                txtDongia.Text = null;
            }

            ClearDataBindings();           
            string _bien = cbTenban.EditValue + "";
            _dtb = _dal.loc_tenban(_bien);
            dsthongke.DataSource = _dtb;
            DataBindingControl();
            if(_dtb.Rows.Count  == 0)
            {

                //cbbxmp.Text = _dd + "";
                ClearDataBindings();
                cbbxmp.Text = _dd + "";
                txtSL.Text = null;
                txtDongia.Text = null;
                txtGG.Text = null;
                cbomamon.Text = null;
                txtThanhtien.Text = null;
                
                cboNV.Text = null;

            }

            else
            {

            }
           
        }

        private void dsthongke_Click(object sender, EventArgs e)
        {
            //DataBindingControl();
        }

        private void cbbxmp_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}
